/*
 * Licensed Materials - Property of tenxcloud.com
 * (C) Copyright 2018 TenxCloud. All Rights Reserved.
 * 2018-10-19  @author lizhen
 */

package registry

import (
	goerr "errors"
	"github.com/jinzhu/gorm"
	"k8s.io/apimachinery/pkg/api/errors"
	"regexp"
)

func GetByClusterID(id string, db *gorm.DB) (h string, err error) {
	var cc cluster
	if err = db.Where("id = ?", id).First(&cc).Error; err != nil {
		return
	}
	if matches := pattern.FindStringSubmatch(cc.Harbor); matches != nil {
		h = matches[2]
		return
	}
	err = NotProperlyConfigured
	return
}

var (
	NotProperlyConfigured = errors.NewInternalError(goerr.New("harbor not properly configured"))
	pattern               = regexp.MustCompile(`^(http://|https://)(?P<harbor>.*)/*$`)
)

type cluster struct {
	ID     string `gorm:"column:id"`
	Harbor string `gorm:"column:related_harbors"`
}

func (cluster) TableName() string {
	return "tenx_clusters"
}
